/// 举报信息相关数据模型
/// 基于MyApplication2项目的ReportInfoBean结构实现

/// 查询举报信息请求模型
class ReportInfoRequest {
  final String reporterId;
  final int page;
  final int timestamp;

  ReportInfoRequest({
    required this.reporterId,
    required this.page,
    required this.timestamp,
  });

  Map<String, dynamic> toJson() {
    return {
      'reporterId': reporterId,
      'page': page,
      'timestamp': timestamp,
    };
  }

  factory ReportInfoRequest.fromJson(Map<String, dynamic> json) {
    return ReportInfoRequest(
      reporterId: json['reporterId']?.toString() ?? '',
      page: json['page']?.toInt() ?? 1,
      timestamp: json['timestamp']?.toInt() ?? 0,
    );
  }
}

/// 举报信息响应模型
class ReportInfoResponse {
  final String status;
  final String? message;
  final ReportInfoData? data;

  ReportInfoResponse({
    required this.status,
    this.message,
    this.data,
  });

  /// 是否成功
  bool get isSuccess => status == 'success';

  factory ReportInfoResponse.fromJson(Map<String, dynamic> json) {
    return ReportInfoResponse(
      status: json['status']?.toString() ?? 'error',
      message: json['message']?.toString(),
      data: json['data'] != null ? ReportInfoData.fromJson(json['data']) : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'status': status,
      'message': message,
      'data': data?.toJson(),
    };
  }
}

/// 举报信息数据模型
class ReportInfoData {
  final List<ReportItem> reports;
  final ReportPagination? pagination;

  ReportInfoData({
    required this.reports,
    this.pagination,
  });

  factory ReportInfoData.fromJson(Map<String, dynamic> json) {
    return ReportInfoData(
      reports: (json['reports'] as List<dynamic>?)
          ?.map((item) => ReportItem.fromJson(item))
          .toList() ?? [],
      pagination: json['pagination'] != null 
          ? ReportPagination.fromJson(json['pagination']) 
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'reports': reports.map((item) => item.toJson()).toList(),
      'pagination': pagination?.toJson(),
    };
  }
}

/// 举报项目数据模型
class ReportItem {
  final int? reportId;
  final String? reportType;
  final int? targetId;
  final String? reportReason;
  final List<String> reportImages;
  final ReportCategory? reportCategory;
  final List<String> reportTags;
  final String? reportLocation;
  final String? reportTime;
  final int? reporterId;
  final String? reporterUsername;
  final String? reporterAvatar;
  final int? targetUserId;
  final String? targetUsername;
  final String? targetAvatar;
  final int? processStatus;
  final String? processResult;
  final String? processTime;
  final int? processorId;
  final ReportStatus? status;
  final Map<String, dynamic>? extraData;
  final String? createdAt;
  final String? updatedAt;

  ReportItem({
    this.reportId,
    this.reportType,
    this.targetId,
    this.reportReason,
    this.reportImages = const [],
    this.reportCategory,
    this.reportTags = const [],
    this.reportLocation,
    this.reportTime,
    this.reporterId,
    this.reporterUsername,
    this.reporterAvatar,
    this.targetUserId,
    this.targetUsername,
    this.targetAvatar,
    this.processStatus,
    this.processResult,
    this.processTime,
    this.processorId,
    this.status,
    this.extraData,
    this.createdAt,
    this.updatedAt,
  });

  /// 获取处理状态文本
  String get processStatusText {
    switch (processStatus) {
      case 0:
        return '待处理';
      case 1:
        return '处理中';
      case 2:
        return '已处理';
      case 3:
        return '已驳回';
      default:
        return '未知状态';
    }
  }

  /// 获取举报类型文本
  String get reportTypeText {
    switch (reportType) {
      case 'user':
        return '举报用户';
      case 'activity':
        return '举报活动';
      case 'comment':
        return '举报评论';
      case 'content':
        return '举报内容';
      default:
        return reportType ?? '未知类型';
    }
  }

  /// 是否已处理
  bool get isProcessed => processStatus == 2;

  /// 是否被驳回
  bool get isRejected => processStatus == 3;

  factory ReportItem.fromJson(Map<String, dynamic> json) {
    return ReportItem(
      reportId: json['reportId']?.toInt(),
      reportType: json['reportType']?.toString(),
      targetId: json['targetId']?.toInt(),
      reportReason: json['reportReason']?.toString(),
      reportImages: (json['reportImages'] as List<dynamic>?)
          ?.map((item) => item.toString())
          .toList() ?? [],
      reportCategory: json['reportCategory'] != null 
          ? ReportCategory.fromJson(json['reportCategory']) 
          : null,
      reportTags: (json['reportTags'] as List<dynamic>?)
          ?.map((item) => item.toString())
          .toList() ?? [],
      reportLocation: json['reportLocation']?.toString(),
      reportTime: json['reportTime']?.toString(),
      reporterId: json['reporterId']?.toInt(),
      reporterUsername: json['reporterUsername']?.toString(),
      reporterAvatar: json['reporterAvatar']?.toString(),
      targetUserId: json['targetUserId']?.toInt(),
      targetUsername: json['targetUsername']?.toString(),
      targetAvatar: json['targetAvatar']?.toString(),
      processStatus: json['processStatus']?.toInt(),
      processResult: json['processResult']?.toString(),
      processTime: json['processTime']?.toString(),
      processorId: json['processorId']?.toInt(),
      status: json['status'] != null 
          ? ReportStatus.fromJson(json['status']) 
          : null,
      extraData: json['extraData'] as Map<String, dynamic>?,
      createdAt: json['created_at']?.toString(),
      updatedAt: json['updated_at']?.toString(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'reportId': reportId,
      'reportType': reportType,
      'targetId': targetId,
      'reportReason': reportReason,
      'reportImages': reportImages,
      'reportCategory': reportCategory?.toJson(),
      'reportTags': reportTags,
      'reportLocation': reportLocation,
      'reportTime': reportTime,
      'reporterId': reporterId,
      'reporterUsername': reporterUsername,
      'reporterAvatar': reporterAvatar,
      'targetUserId': targetUserId,
      'targetUsername': targetUsername,
      'targetAvatar': targetAvatar,
      'processStatus': processStatus,
      'processResult': processResult,
      'processTime': processTime,
      'processorId': processorId,
      'status': status?.toJson(),
      'extraData': extraData,
      'created_at': createdAt,
      'updated_at': updatedAt,
    };
  }
}

/// 举报分类模型
class ReportCategory {
  final int? id;
  final String? name;
  final String? description;

  ReportCategory({
    this.id,
    this.name,
    this.description,
  });

  factory ReportCategory.fromJson(Map<String, dynamic> json) {
    return ReportCategory(
      id: json['id']?.toInt(),
      name: json['name']?.toString(),
      description: json['description']?.toString(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'description': description,
    };
  }
}

/// 举报状态模型
class ReportStatus {
  final String? code;
  final String? message;

  ReportStatus({
    this.code,
    this.message,
  });

  factory ReportStatus.fromJson(Map<String, dynamic> json) {
    return ReportStatus(
      code: json['code']?.toString(),
      message: json['message']?.toString(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'code': code,
      'message': message,
    };
  }
}

/// 举报分页信息模型
class ReportPagination {
  final int? total;
  final int? page;
  final int? pageSize;
  final int? totalPages;

  ReportPagination({
    this.total,
    this.page,
    this.pageSize,
    this.totalPages,
  });

  /// 是否有下一页
  bool get hasNextPage => page != null && totalPages != null && page! < totalPages!;

  /// 是否有上一页
  bool get hasPreviousPage => page != null && page! > 1;

  factory ReportPagination.fromJson(Map<String, dynamic> json) {
    return ReportPagination(
      total: json['total']?.toInt(),
      page: json['page']?.toInt(),
      pageSize: json['page_size']?.toInt(),
      totalPages: json['total_pages']?.toInt(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'total': total,
      'page': page,
      'page_size': pageSize,
      'total_pages': totalPages,
    };
  }
}

/// 创建举报请求模型
class CreateReportRequest {
  final int reporterId;
  final String reportType;
  final String reportReason;
  final int? reportedUserId;
  final int? reportedContentId;
  final String? description;
  final List<String>? reportImages;
  final String? reportLocation;

  CreateReportRequest({
    required this.reporterId,
    required this.reportType,
    required this.reportReason,
    this.reportedUserId,
    this.reportedContentId,
    this.description,
    this.reportImages,
    this.reportLocation,
  });

  Map<String, dynamic> toJson() {
    final data = <String, dynamic>{
      'reporter_id': reporterId,
      'report_type': reportType,
      'report_reason': reportReason,
    };

    if (reportedUserId != null) {
      data['reported_user_id'] = reportedUserId;
    }
    
    if (reportedContentId != null) {
      data['reported_content_id'] = reportedContentId;
    }
    
    if (description != null && description!.isNotEmpty) {
      data['description'] = description;
    }
    
    if (reportImages != null && reportImages!.isNotEmpty) {
      data['report_images'] = reportImages;
    }
    
    if (reportLocation != null && reportLocation!.isNotEmpty) {
      data['report_location'] = reportLocation;
    }

    return data;
  }

  factory CreateReportRequest.fromJson(Map<String, dynamic> json) {
    return CreateReportRequest(
      reporterId: json['reporter_id']?.toInt() ?? 0,
      reportType: json['report_type']?.toString() ?? '',
      reportReason: json['report_reason']?.toString() ?? '',
      reportedUserId: json['reported_user_id']?.toInt(),
      reportedContentId: json['reported_content_id']?.toInt(),
      description: json['description']?.toString(),
      reportImages: (json['report_images'] as List<dynamic>?)
          ?.map((item) => item.toString())
          .toList(),
      reportLocation: json['report_location']?.toString(),
    );
  }
}

/// 创建举报响应模型
class CreateReportResponse {
  final String status;
  final String? message;
  final int? reportId;

  CreateReportResponse({
    required this.status,
    this.message,
    this.reportId,
  });

  /// 是否成功
  bool get isSuccess => status == 'success';

  factory CreateReportResponse.fromJson(Map<String, dynamic> json) {
    return CreateReportResponse(
      status: json['status']?.toString() ?? 'error',
      message: json['message']?.toString(),
      reportId: json['report_id']?.toInt(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'status': status,
      'message': message,
      'report_id': reportId,
    };
  }
}
